package reduce.fz.com.reduce.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.SocietyNotice;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class SocietyNoticeFragment extends BaseListFragment<SocietyNoticeFragment.NoticeViewHolder, SocietyNotice> {
    private Long sid;

    public  static SocietyNoticeFragment create(Long sid) {
        SocietyNoticeFragment fragment = new SocietyNoticeFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("sid", sid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sid = getArguments().getLong("sid");
    }


    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<SocietyNotice>>(new TypeReference<Result<List<SocietyNotice>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<SocietyNotice>> data) {
                addResult(data);
            }
        });
    }


    public SocietyNoticeFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    @Override
    protected NoticeViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_notice_item, parent, false);
        NoticeViewHolder viewHolder = new NoticeViewHolder(view);
        return viewHolder;
    }

    @Override
    protected NoticeViewHolder createfootViewHolder(View view) {
        return new NoticeViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<SocietyNotice> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_society_notice, sid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }

    public static class NoticeViewHolder extends BaseRecycleViewHolder<SocietyNotice> {
        private TextView mDesc;
        private TextView mPubtime;

        public NoticeViewHolder(View itemView) {
            super(itemView);
            mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
            mPubtime = (TextView) itemView.findViewById(R.id.tv_pub_time);

        }

        @Override
        public void renderView(SocietyNotice data, int position) {
            mDesc.setText(data.description);
            mPubtime.setText(DateUtil.getInterval(data.pubtime));

        }
    }


}
